﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace USERS.CYT369.COM.DAL.DbContexts
{
    public partial class CYT369Context : Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<USERS.CYT369.COM.MODELS.SysUser,
                                                                                                        USERS.CYT369.COM.MODELS.UserType,
                                                                                                        int,
                                                                                                        USERS.CYT369.COM.MODELS.sys.UserClaim,
                                                                                                        USERS.CYT369.COM.MODELS.sys.UserRole,
                                                                                                        USERS.CYT369.COM.MODELS.sys.UserLogin,
                                                                                                        USERS.CYT369.COM.MODELS.sys.RoleClaim,
                                                                                                        USERS.CYT369.COM.MODELS.sys.UserToken>
    {
        private USERS.CYT369.COM.UTILITY.Connection m_Connection;
        public CYT369Context()
        {
            m_Connection = USERS.CYT369.COM.UTILITY.SystemSettingService.Instance.UsersDB;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (m_Connection != null)
            {
                if (m_Connection.DBType == USERS.CYT369.COM.UTILITY.ConstStringClass.SQLSERVER2008 ||
                    m_Connection.DBType == USERS.CYT369.COM.UTILITY.ConstStringClass.SQLSERVER2005)
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
                }
                else
                {
                    optionsBuilder.UseSqlServer(m_Connection.ConnectionString);
                }
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            string schema = null;
            if (m_Connection != null)
            {
                schema = m_Connection.DBSchema;
            }

            AppModelCreating(builder, schema);
            BaseModelCreating(builder, schema);
        }
        private void AppModelCreating(ModelBuilder modelBuilder, string schema)
        {
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.App>().ToTable("TB_App", schema);
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.UserLoginLog>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("TB_UserLoginLog", schema);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Terms.Terms>().ToTable("TB_Terms", schema);
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Terms.TermsType>().ToTable("TB_TermsType", schema);
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.Terms.TermsFile>().ToTable("TB_TermsFile", schema);


            modelBuilder.Entity<USERS.CYT369.COM.MODELS.UserConfig>().ToTable("TB_UserConfig", schema).HasOne(c=>c.User).WithMany(c=>c.Configs).HasForeignKey(c=>c.UserID);
        }


        private void BaseModelCreating(ModelBuilder modelBuilder, string schema)
        {
            modelBuilder.Entity<USERS.CYT369.COM.MODELS.SysUser>(b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
                b.ToTable("TB_User", schema);
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);
                b.Property(u => u.Remark).HasMaxLength(512);
                b.Ignore(u => u.LockoutEnd);
                b.HasQueryFilter(c => c.Deleted == false);


                // Replace with b.HasMany<IdentityUserClaim>().
                b.HasMany<USERS.CYT369.COM.MODELS.sys.UserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany<USERS.CYT369.COM.MODELS.sys.UserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany<USERS.CYT369.COM.MODELS.sys.UserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.sys.UserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("sys_UserClaims", schema);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.sys.UserRole>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("sys_UserRoles", schema);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.sys.UserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
                b.ToTable("sys_UserLogins", schema);
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.sys.UserToken>(b =>
            {
                b.HasKey(l => new { l.UserId, l.LoginProvider, l.Name });
                b.ToTable("sys_UserToken", schema);
            });


            modelBuilder.Entity<USERS.CYT369.COM.MODELS.SysUser>(b =>
            {
                b.HasMany<USERS.CYT369.COM.MODELS.sys.UserRole>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.UserType>(b =>
            {
                b.HasKey(r => r.Id);
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();
                b.ToTable("TB_UserType", schema);
                b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.Name).HasMaxLength(256);
                b.Property(u => u.NormalizedName).HasMaxLength(256);

                b.HasMany<USERS.CYT369.COM.MODELS.sys.UserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
                b.HasMany<USERS.CYT369.COM.MODELS.sys.RoleClaim>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
            });

            modelBuilder.Entity<USERS.CYT369.COM.MODELS.sys.RoleClaim>(b =>
            {
                b.HasKey(rc => rc.Id);
                b.ToTable("sys_RoleClaims", schema);
            });
        }


    }
}
